﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace Payroll
{
    public partial class LoginForm : System.Windows.Forms.Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.AcceptButton = button1;
        }

        private void button1_Click(object sender, EventArgs e)//登录
        {
            string name = textBox1.Text;
            string password = textBox2.Text;

            if (Regex.IsMatch(name, "^[A-Za-z0-9_\u4e00-\u9fa5]{3,10}$"))//用户名需满足：长度3到10，允许汉字、字母、数字、下划线
            {
                if (Regex.IsMatch(password, "^[a-zA-Z][a-zA-Z0-9_]{5,15}$"))//密码需满足：长度6到15，字母开头，允许数字、子母
                {
                    string sqlStr;
                    string connString = @"Data Source=..\..\Database1.sdf";
                    SqlCeConnection connection = new SqlCeConnection(connString);
                    connection.Open();
                    sqlStr = "select * from LoginUser where Username = '" + name + "'";
                    SqlCeCommand cmd = new SqlCeCommand(sqlStr, connection);
                    SqlCeDataReader result = cmd.ExecuteReader();
                    if (result.Read())//检验用户是否已存在
                    {
                        if (password == Convert.ToString(result[2]))//检验密码
                        {
                            myUser.Name = name;
                            MessageBox.Show("登录成功，欢迎用户：" + name, "提示");
                            //登录成功跳转界面
                            Mainform f = new Mainform();
                            f.StartPosition = FormStartPosition.CenterScreen;
                            f.Show();
                            this.Opacity = 0;//把登录界面隐藏

                            myFile.myWrite_operate(myUser.Name,"登录");
                        }
                        else
                            MessageBox.Show("密码错误！请重新输入！","警告");
                            
                    }
                    else
                        MessageBox.Show("该用户不存在！请先注册！", "警告");
                    connection.Close();
                }
                else
                    MessageBox.Show("密码需满足：长度6到15，字母开头，允许数字、字母！  输入不符合条件！", "警告");
            }
            else
                MessageBox.Show("用户名需满足：长度3到10，允许汉字、字母、数字、下划线！ 输入不符合条件！", "警告");
        }

        private void button2_Click(object sender, EventArgs e)//注册
        {
            string name = textBox1.Text;
            string password = textBox2.Text;

            if (Regex.IsMatch(name, "^[A-Za-z0-9_\u4e00-\u9fa5]{3,10}$"))//用户名需满足：长度3到10，允许汉字、字母、数字、下划线
            {
                if (Regex.IsMatch(password, "^[a-zA-Z][a-zA-Z0-9_]{5,15}$"))//密码需满足：长度6到15，字母开头，允许数字、子母
                {
                    string sqlStr;
                    string connString = @"Data Source=..\..\Database1.sdf";
                    SqlCeConnection connection = new SqlCeConnection(connString);
                    connection.Open();
                    sqlStr = "select * from LoginUser where Username = '" + name + "'";
                    SqlCeCommand cmd = new SqlCeCommand(sqlStr, connection);
                    SqlCeDataReader result = cmd.ExecuteReader();
                    while (result.Read())//检验用户是否已存在
                    {
                        MessageBox.Show("该用户名已存在！请重新注册！", "警告");
                        return;
                    }

                    sqlStr = " insert into LoginUser (Username,Password) VALUES ('" + name + "','" + password + "')";
                    SqlCeCommand cmd2 = new SqlCeCommand(sqlStr, connection);
                    int result2 = cmd2.ExecuteNonQuery();

                    if (result2 == 1) MessageBox.Show("注册成功!", "提示");
                    connection.Close();
                }
                else
                    MessageBox.Show("密码需满足：长度6到15，字母开头，允许数字、字母！  输入不符合条件！", "警告");
            }
            else
                MessageBox.Show("用户名需满足：长度3到10，允许汉字、字母、数字、下划线！ 输入不符合条件！", "警告");
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            //  退出失去焦点，重新显示
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                textBox1.ForeColor = Color.DarkGray;
                this.textBox1.Text = "汉字,字母,数字,下划线";
            }
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            //  进入获得焦点，清空
            if (textBox1.Text == "汉字,字母,数字,下划线")
            {
                textBox1.ForeColor = Color.Black;
                this.textBox1.Text = "";
            }
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            //退出失去焦点，重新显示
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                this.textBox2.PasswordChar = default(char);//设置密码框显示字符为*
                textBox2.ForeColor = Color.DarkGray;
                this.textBox2.Text = "字母开头，允许数字，字母";
            }
        }

        private void textBox2_Enter(object sender, EventArgs e)
        {
            //  进入获得焦点，清空
            if (textBox2.Text == "字母开头，允许数字，字母")
            {
                textBox2.PasswordChar = char.Parse("*"); //设置密码框显示字符为*
                textBox2.ForeColor = Color.Black;
                this.textBox2.Text = "";
            }
        }
    }
}
